package com.wantong.jianpu.customviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;

import com.wantong.jianpu.R;

import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * @author PACY
 * @date 2022/4/25
 * description:
 */
public class TopSelectView extends LinearLayout {
    String content;
    String hintText;

    TextView contentTv;


    public TopSelectView(Context context) {
        super(context);
    }

    public TopSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        /**
         * 加载布局  获取自定义属性
         */
        LayoutInflater.from(context).inflate(R.layout.top_select_view, this, true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopSelectView);
        content = a.getString(R.styleable.TopSelectView_topSelectContent);
        hintText = a.getString(R.styleable.TopSelectView_topSelectHint);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        contentTv = findViewById(R.id.content);
        if (StringUtils.isNotEmpty(content)) {
            setContent(content);
        }
        if (StringUtils.isNotEmpty(hintText)) {
            setHintText(hintText);
        }
    }

    /**
     * 设置内容
     */
    public void setContent(String content) {
        contentTv.setText(content);

    }

    /**
     * 设置hint
     */
    public void setHintText(String hintText) {
        contentTv.setHint(hintText);
    }



    @BindingAdapter("topSelectContent")
    public static void setStrContent(TopSelectView topSelectView, String content) {
        if (topSelectView != null) {
            String mCurrentStr = topSelectView.contentTv.getText().toString().trim();
            if (StringUtils.isNotEmpty(content)) {
                if (!content.equalsIgnoreCase(mCurrentStr)) {
                    topSelectView.contentTv.setText(content);
                }
            }
        }
    }
}
